package com.java.star.mall.front.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.java.star.mall.common.response.StatusCode;
import com.java.star.mall.common.utils.Asserts;
import com.java.star.mall.front.pojo.authentication.CurrentPrincipal;
import com.java.star.mall.front.pojo.param.AdvancedOrderParam;
import com.java.star.mall.front.pojo.param.WechatPayParam;
import com.java.star.mall.front.pojo.vo.OrderVO;
import com.java.star.mall.front.service.OrderService;
import com.java.star.mall.front.service.WechatPayService;
import com.java.star.mall.front.util.CurrentPrincipalUtils;
import com.java.star.mall.front.util.RestTemplateUtils;
import com.java.star.mall.front.util.WxSigUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
@Slf4j
public class WechatPayServiceImpl implements WechatPayService {

    @Autowired
    private OrderService orderService;

    @Override
    public void generateAdvancedOrder(AdvancedOrderParam advancedOrderParam) {
        //效验当前要支付的订单是否存在和金额
        OrderVO orderVO = orderService.getOrderByOrderSn(advancedOrderParam.getOrderSn());
        Asserts.isTrue(orderVO == null, StatusCode.PAY_ORDER_NOT_EXIST);
        Asserts.isTrue(advancedOrderParam.getPayAmount().equals(orderVO.getPayAmount()), StatusCode.PAY_AMOUNT_NOT_MATCH);

        //装填微信支付接口需要的参数
        WechatPayParam wechatPayParam = loadingWechatPayParam(orderVO);

        //计算签名
        calcSig(wechatPayParam);

        //发起调用
        sendPayRequest(wechatPayParam);
    }

    /**
     * 装填微信支付接口需要的参数
     *
     * @param orderVo 订单信息
     * @return 微信支付参数
     */
    private WechatPayParam loadingWechatPayParam(OrderVO orderVo) {
        WechatPayParam wechatPayParam = new WechatPayParam();
        wechatPayParam.setAppId("wxcc651fcbab275e33");
        wechatPayParam.setMchId("1481962542");
        wechatPayParam.setNonceStr(RandomUtil.randomStringUpper(32));
        wechatPayParam.setBody("星辰外设屋-商品购买");
        wechatPayParam.setOutTradeNo(orderVo.getOrderSn());
        wechatPayParam.setTotalFee((orderVo.getPayAmount().multiply(new BigDecimal(100)).intValue()));
        wechatPayParam.setSpbillCreateIp("127.0.0.1");
        wechatPayParam.setNotifyUrl("https://tk52856365.oicp.vip/pay/notify");
        wechatPayParam.setTradeType("JSAPI");
        CurrentPrincipal currentPrincipal = CurrentPrincipalUtils.getCurrentPrincipal();
        wechatPayParam.setOpenId(currentPrincipal.getOpenId());
        return wechatPayParam;
    }

    /**
     * 计算签名
     *
     * @param wechatPayParam 微信参数
     */
    private void calcSig(WechatPayParam wechatPayParam) {
        String wxPaySig = WxSigUtils.wxPaySig("MXb72b9RfshXZD4FRGV5KLqmv5bx9LT9", wechatPayParam);
        log.debug("微信支付的签名：{}", wxPaySig);
        //设置签名
        wechatPayParam.setSign(wxPaySig);
    }

    private void sendPayRequest(WechatPayParam wechatPayParam) {
        String result = RestTemplateUtils.post("https://api.mch.weixin.qq.com/pay/unifiedorder", wechatPayParam, MediaType.APPLICATION_XML);
        log.debug("预付单的响应结果：{}", result);
    }
}
